<template>
  <div id="login">
    <div id="center">
      <div class="c-box">
        <div class="c-box-box">
          <img src="../assets/logo.png" alt />
        </div>
      </div>
      <section>
        <el-form ref="form" :model="obj" :rules="rules">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入你的用户名"
              prefix-icon="el-icon-user-solid"
              v-model="obj.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入你的密码 "
              prefix-icon="el-icon-s-order"
              v-model="obj.password"
              type="password"
            ></el-input>
          </el-form-item>

          <el-form-item class="right">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </section>
    </div>
  </div>
</template> 
<script>
export default {
  name: "login",
  data() {
    return {
      obj: {
        username: "admin",
        password: "123456"
      },
      // 表单验证的规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    login() {
      // console.log(this.$refs.form)
      // async
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          //调接 口
          // alert('submit!');
          // let res =await this.$http.post("/login",this.obj)

          //data:res中 res是data的命名直接data也可以
          let { data:res } = await this.$http.post("/login", this.obj);
          if (res.meta.msg == "登录成功") {
            //提示用户登录成功,存储token到sessionStorage中
            sessionStorage.setItem("loginmsg", JSON.stringify(res.data));

            this.$message({
              message: "恭喜你,登录成功",
              type: "success"
            });
            this.$router.push("/home");
          } 
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    reset() {
      this.$refs.form.resetFields();
    }
  },
};
</script>
<style lang="scss" scoped>
#login {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  #center {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px #ddd;
    width: 450px;
    height: 304px;
    border-radius: 3px;
    background: #fff;
    .c-box {
      position: absolute;
      top: -25%;
      left: 33%;
      width: 130px;
      height: 130px;
      background: white;
      padding: 10px;
      border: 1px solid #efefef;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .c-box-box {
        width: 110px;
        height: 110px;
        padding: 10px;
        background: #cccccc;
        border: 1px solid #cccccc;
        border-radius: 50%;
        img {
          width: 100%;
          height: 100%;
          border-radius: 40%;
        }
      }
    }
    section {
      padding: 20px;
      padding-top: 100px;
    }
  }
}
.right {
  float: right;
}
</style>
